/**
 * Created by woziji00226 on 2024-11-28 13:06:09.
 * 木头对象的显示对象
 */



/**
 * 代表木头的状态
 */
enum WoodStatus_W26 {
    Float,//浮动
    Down,//下落
    Static,//静止
}

/**
 * 木头运动方向速度
 */
enum WoodSpeed_W26 {
    LeftToRight = 1,
    RightToLeft = -1
}

class GameSprite_Wood_W26 extends GUI_15002 {

    //原始数据记录
    private _originalProperty: {
        woodLeft: {
            pos: { x: number, y: number },
            wh: { width: number, height: number }
        },
        woodRight: {
            pos: { x: number, y: number },
            wh: { width: number, height: number }
        }
        woodCenter: {
            pos: { x: number, y: number },
            wh: { width: number, height: number }
        }
    } = {} as any;

    private _originalWidth: number = 0;

    //记录木头两侧的相对位置
    private _relativeLeftX: number;
    private _relativeLeftY: number;
    private _relativeRightX: number;
    private _relativeRightY: number;

    private _woodListIndex: number;

    /**
     * 速度方向 1是向右  -1是向左
     */
    private _speed: number = 1;

    private _status: WoodStatus_W26 = WoodStatus_W26.Static;

    /**
     * 构造函数
     */
    constructor() {
        super();
        this.init();
    }

    /**
     * 初始化主要是要初始化木头的一些属性
     */
    private init(): void {
        //记录木头两侧的相对位置
        this._originalProperty.woodLeft = {
            pos: { x: this.wood_left.x, y: this.wood_left.y },
            wh: { width: this.wood_left.width, height: this.wood_left.height }
        };
        this._originalProperty.woodRight = {
            pos: { x: this.wood_right.x, y: this.wood_right.y },
            wh: { width: this.wood_right.width, height: this.wood_right.height }
        };
        this._originalProperty.woodCenter = {
            pos: { x: this.wood_visiable.x, y: this.wood_visiable.y },
            wh: { width: this.wood_visiable.width, height: this.wood_visiable.height }
        };


        //左侧相对X
        this._relativeLeftX = this._originalProperty.woodLeft.pos.x - this._originalProperty.woodCenter.pos.x;
        this._relativeLeftY = this._originalProperty.woodLeft.pos.y - this._originalProperty.woodCenter.pos.y;

        //
        this._relativeRightX = this._originalProperty.woodRight.pos.x - (this._originalProperty.woodCenter.pos.x + this._originalProperty.woodCenter.wh.width);
        this._relativeRightY = this._originalProperty.woodRight.pos.y - (this._originalProperty.woodCenter.pos.y + this._originalProperty.woodCenter.wh.height);

        this._originalWidth = this.woodWidth;
    }

    /**
     * 刷新木头两边的位置，以便于木头两边永远会有显示
     */
    private refreshWoodLeftRightPos(): void {

        this.wood_left.x = this._relativeLeftX + this.wood_visiable.x;
        this.wood_left.y = this._relativeLeftY + this.wood_visiable.y;

        this.wood_right.x = this._relativeRightX + (this.wood_visiable.x + this.wood_visiable.width);
        this.wood_right.y = this._relativeRightY + (this.wood_visiable.y + this.wood_visiable.height);
    }

    /**
     * 设置木头图片
     * @param woodData 
     */
    public setWoodSource(woodDataIndex: number): void {
        this._woodListIndex = woodDataIndex;
        this.wood_left.image = WorldData.woodList[woodDataIndex].wood_1;
        this.wood_center.image = WorldData.woodList[woodDataIndex].wood_2;
        this.wood_right.image = WorldData.woodList[woodDataIndex].wood_3;
    }

    /**
     * 减去一个木头
     * @param targetWood 
     * @return [GUI_Wood] 被切下的木头
     */
    public subtract(targetWood: GameSprite_Wood_W26): GameSprite_Wood_W26 {
        //误差 可以接受该范围像素的偏差
        var errorRate = 2;

        if (this.x <= targetWood.x) {
            //相差值
            //  x-----|---
            //      tx|--------
            //相差值 = tx - x
            var splitX = targetWood.x - this.x;
            if (splitX > this.woodWidth) {
                splitX = this.woodWidth;
            }
            if (splitX < errorRate) {
                return null;
            }
            return this.splitWoodFromLeft(splitX);
        }
        else {
            //相差值
            //        x----|-----
            //  tx---------|
            //相差值 = (tx+tw)-x
            var splitX = (targetWood.woodWidth + targetWood.x) - this.x;
            if (splitX < 0) {
                splitX = 0;
            }
            if (splitX > this.x + this.woodWidth - errorRate) {
                return null
            }
            return this.splitWoodFromRight(splitX);
        }
    }

    /**
     * 从左边开始切割木头
     *     x-----|---
     *         tx|--------
     * @param splitX 
     * @return [GUI_Wood_W26] 
     */
    private splitWoodFromLeft(splitX: number): GameSprite_Wood_W26 {
        //没切割前的位置
        var x = this.x;
        var width = this.woodWidth;

        if (splitX > x + this.woodWidth) {
            splitX = this.woodWidth;
        }

        //切割的位置 x
        this.woodSplitX = splitX;
        //剩下的宽度
        this.woodWidth = this.woodWidth - splitX;

        var newWood = new GameSprite_Wood_W26();
        newWood.woodSplitX = 0;
        newWood.woodWidth = splitX;
        newWood.x = x;
        newWood.y = this.y;
        newWood.setWoodSource(this.woodListIndex);
        return newWood;
    }

    /**
     * 从右边开始切割木头
     *         x----|-----
     *   tx---------|
     * @param spliteX 
     * @return [GUI_Wood_W26] 
     */
    private splitWoodFromRight(splitX: number): GameSprite_Wood_W26 {
        var x = this.x;
        var width = this.woodWidth;

        if (splitX < 0) {
            splitX = 0;
        }

        this.woodSplitX = 0;
        this.woodWidth = splitX;

        var newWood = new GameSprite_Wood_W26();
        newWood.x = x;
        newWood.y = this.y;
        newWood.setWoodSource(this.woodListIndex);
        //console.log(splitX);
        //切割的位置 x
        newWood.woodSplitX = splitX;
        //剩下的宽度
        newWood.woodWidth = width - splitX;

        return newWood;
    }


    /**
     * 木头宽度
     */
    public get woodWidth() {
        return this.wood_visiable.width;
    }

    /**
     * 木头高度
     */
    public get woodHeight() {
        return this.wood.height;
    }

    /**
     * 木头切割点
     */
    public get woodSplitX() {
        return this.wood_visiable.x;
    }

    /**
     * 改变木头切割点
     * @param value 
     */
    public set woodSplitX(value: number) {
        //先把可视范围向右移动到切割点
        this.wood_visiable.x = value;
        //把木头显示的位置向左移动，移动的距离就是被切掉的宽度，这部分不可视
        this.wood_center.x = -value;
        //整体坐标向右移动 得到正确的x位置
        this.x += value;
        //可视范围向左移动 显示正常
        this.wood_visiable.x -= value;
        //刷新左右两边木头图片
        this.refreshWoodLeftRightPos();
    }

    /**
     * 改变木头宽度
     * @param value 
     */
    public set woodWidth(value: number) {
        this.wood_visiable.width = value;
        this.refreshWoodLeftRightPos();
    }

    /**
     * 改变木头高度
     * @param value 
     */
    public set woodHeight(value: number) {
        this.wood.height = value;
        this.refreshWoodLeftRightPos();
    }

    /**
     * 速度方向
     * @return [number] 
     */
    public get speed(): number {
        return this._speed;
    }

    /**
     * 速度方向
     * @param value 
     */
    public set speed(value: number) {
        this._speed = value;
    }

    /**
     * 状态
     * @return [WoodStatus_W26] 
     */
    public get status(): WoodStatus_W26 {
        return this._status;
    }

    /**
     * 状态
     * @param value 
     */
    public set status(value: WoodStatus_W26) {
        this._status = value;
    }


    public get woodListIndex() {
        return this._woodListIndex;
    }

    /**
     * 获取切割比例
     * @return [number] 
     */
    public get splitPer(): number {
        if (this._originalWidth == 0) {
            return 0;
        }
        return this.woodWidth / this._originalWidth;
    }


    public set originalWidth(value: number) {
        this._originalWidth = value;
    }
}




